package main

import (
	"fmt"
	"io"
	"os"
)

func main() {
	srcFile, err := os.Open("./z_demo/demo_os_io_buifo/demo05/source.txt")
	if err != nil {
		fmt.Println("Error opening source file:", err)
		return
	}
	defer srcFile.Close()

	destFile, err := os.Create("./z_demo/demo_os_io_buifo/demo05/destination.txt")
	if err != nil {
		fmt.Println("Error creating destination file:", err)
		return
	}
	defer destFile.Close()

	written, err := io.Copy(destFile, srcFile)
	if err != nil {
		fmt.Println("Error copying data:", err)
		return
	}

	fmt.Printf("Copied %d bytes.\n", written)

	srcFile.Seek(0, io.SeekStart)
	demo := io.LimitReader(srcFile, 20)
	buffer := make([]byte, 1024)
	_, err = demo.Read(buffer)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(buffer))
}
